package com.xinpu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinpu.entity.TDepartment;
import com.xinpu.entity.TSales;
import com.xinpu.mapper.DeptMapper;
import com.xinpu.service.*;
import com.xinpu.vo.ResponseMessage;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dept")
@Api(tags = "部门有关接口")
public class DepartmentController {

    @Resource
    private ITDepartmentService itDepartmentService;
    @Resource
    private ITSalesService itSalesService;

    @Resource
    private DepartmentService departmentService;


    /**
     * 部门
     *
     * @return
     */
    @GetMapping("list")
    public ResponseMessage list(
            @RequestParam("page") Integer page,
            @RequestParam("size") Integer size
    ) {
        return ResponseMessage.ok(departmentService.getList(new Page<>(page, size), OwnService.getLoginer().getTenantId()));
    }

    /**
     * 保存 编辑
     *
     * @param map
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public ResponseMessage saveOrUpdate(@RequestBody Map<String, String> map) {
        TDepartment tDepartment = new TDepartment();
        String id = map.get("id");
        String name = map.get("name");
        if (StringUtils.isNotBlank(id)) {
            tDepartment.setId(new Integer(map.get("id")));
            QueryWrapper<TDepartment> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name", name);
            TDepartment tDepartment1 = itDepartmentService.getOne(queryWrapper);
            if (tDepartment1 != null) {
                if (!tDepartment1.getId().toString().equals(id)) {
                    return ResponseMessage.error("部门已存在,请勿重复提交");
                }
            }
        } else {
            QueryWrapper<TDepartment> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name", name);
            TDepartment tDepartment1 = itDepartmentService.getOne(queryWrapper);
            if (tDepartment1 != null) {
                return ResponseMessage.error("部门已存在,请勿重复创建");
            }
        }
        tDepartment.setName(name);
        tDepartment.setUserName(map.get("userName"));
        return ResponseMessage.ok(itDepartmentService.saveOrUpdate(tDepartment));
    }

    /**
     * 删除
     *
     * @param map
     * @return
     */
    @PostMapping("/remove")
    public ResponseMessage remove(@RequestBody Map<String, Integer> map) {
        Integer id = map.get("id");
        QueryWrapper<TSales> wrapper = new QueryWrapper<>();
        wrapper.eq("did", id);
        List<TSales> list = itSalesService.list(wrapper);
        if (list.size() == 0) {
            return ResponseMessage.ok(itDepartmentService.removeById(id));
        }
        return ResponseMessage.error("此部门还存在员工,禁止删除");
    }


}

